package org.lfs.config;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.*;

public class PopularProjects {

    public static void showPopularProjects() {
        String jdbcUrl = "jdbc:mysql://192.168.0.105:3306/your_database_name?connectTimeout=2000"; // 连接超时时间设置为2秒
        String username = "admin";
        String password = "ie9rb89FLi";

        // SQL查询模板
        String sqlQuery = "SELECT project_name, description, star_count, download_link FROM popular_projects"; // 替换表名和列名为具体的

        DefaultTableModel model = new DefaultTableModel(new String[]{"项目名", "简介", "star数量", "下载链接"}, 0);
        
        try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sqlQuery)) {

            while (rs.next()) {
                String projectName = rs.getString("project_name");
                String description = rs.getString("description");
                int starCount = rs.getInt("star_count");
                String downloadLink = rs.getString("download_link");
                model.addRow(new Object[]{projectName, description, starCount, downloadLink});
            }
            
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "数据库连接或查询时发生错误");
        }

        JTable table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(600, 300));

        JOptionPane.showMessageDialog(null, scrollPane, "热门项目", JOptionPane.INFORMATION_MESSAGE);
    }



    public static void showProjectSearchDialog() {
        JPanel panel = new JPanel(new GridLayout(4, 2));

        JComboBox<String> repoBox = new JComboBox<>(new String[]{"github", "gitee"});
        JComboBox<String> languageBox = new JComboBox<>(new String[]{
                "Java", "Python", "JavaScript", "Html", "PHP", "GO", "C#", "C++", "CSS"
        });

        JTextField keywordField = new JTextField();

        panel.add(new JLabel("选择仓库:"));
        panel.add(repoBox);
        panel.add(new JLabel("选择语言:"));
        panel.add(languageBox);
        panel.add(new JLabel("关键字:"));
        panel.add(keywordField);

        int result = JOptionPane.showConfirmDialog(null, panel, "搜索热门项目", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String repository = (String) repoBox.getSelectedItem();
            String language = (String) languageBox.getSelectedItem();
            String keyword = keywordField.getText().trim();

            if (keyword.length() > 20) {
                JOptionPane.showMessageDialog(null, "关键字长度不能超过20个字符");
                return;
            }

            try {
                String encodedKeyword = URLEncoder.encode(keyword, StandardCharsets.UTF_8.toString());
                String url = buildSearchUrl(repository, language, encodedKeyword);
                Desktop.getDesktop().browse(new URI(url));
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "无法打开浏览器: " + e.getMessage());
            }
        }
    }

    private static String buildSearchUrl(String repository, String language, String keyword) {
        String baseUrl;
        if ("github".equals(repository)) {
            baseUrl = "https://github.com/search?q=" + keyword + "&l=" + language + "&type=repositories&s=stars&o=desc";
        } else {
            baseUrl = "https://so.gitee.com/?q=" + keyword + "&l=" + language + "&type=repositories&s=stars&o=desc";
        }
        return baseUrl;
    }
}
